﻿using System;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Reflection;
using System.Collections;
namespace BLL
{
    public static class MediaForecast
    {
        private static Data.MediaForecast Data_Newscont = new Data.MediaForecast();

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static long Add(Model.MediaForecast model)
        {
            return Data_Newscont.Add(model);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int Update(Model.MediaForecast model)
        {
            return Data_Newscont.Update(model);
        }

        //public static int Update(string sql,string canshu)
        //{

        //}
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static bool Delete(long Id)
        {
            return Data_Newscont.Delete(Id);
        }
        /// <summary>
        /// 单条
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static Model.MediaForecast GetModel(long Id)
        {
            return Data_Newscont.GetModel(Id);
        }
        public static Model.MediaForecast GetModelBysql(string sql)
        {
            return Data_Newscont.GetModelBysql(sql);
        }

        /// <summary>
        /// 总数据数
        /// </summary>
        /// <returns></returns>
        public static int GetAll_Count()
        {
            return Data_Newscont.GetAll_Count();
        }
        /// <summary>
        /// 抽取分页数据
        /// </summary>
        public static List<Model.MediaForecast> GetPageList_Where(int pageIndex, int pageSize, string sqlwhere, out int totalCount)
        {
            return Data_Newscont.GetPageList_Where(pageIndex, pageSize, sqlwhere, out totalCount);
        }
        public static List<Model.MediaForecast> GetPageList_Where1(int pageIndex, int pageSize, string sqlwhere)
        {
            return Data_Newscont.GetPageList_Where1(pageIndex, pageSize, sqlwhere);
        }
        public static DataTable GetPageDT_Where(int PageIndex, int PageSize, string sqlwhere, out int totalCount)
        {
            return Data_Newscont.GetPageDT_Where(PageIndex, PageSize, sqlwhere, out totalCount);
        }

        public static int GetTotalCount(string sqlwhere)
        {
            return Data_Newscont.GetTotalCount(sqlwhere);
        }
        /// <summary>
        /// 批删
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public static int Drop(string Ids)
        {
            return Data_Newscont.Drop(Ids);
        }
        //------------------------------扩展-----------------------------------------
        public static int CreateForecast(long userid, string username, int lotteryType, string lotteryQs, string ForecastNo)
        {
            return Data_Newscont.CreateForecast(userid, username, lotteryType, lotteryQs, ForecastNo);
        }
        public static List<Model.MediaForecast> GetList(string Sql)
        {
            return Data_Newscont.GetList(Sql);
        }
        public static DataTable GetPageListBysql(string sql)
        {
            return Data_Newscont.GetPageListBysql(sql);
        }

        //public static List<Model.MediaForecaststatistics> GetMediaForecaststatistics(int lotteryType,string lotteryQs)
        //{
        //    return Data_Newscont.GetMediaForecaststatistics(lotteryType, lotteryQs);
        //}
        //====================================晚上定制执行==========================================
        public static void UpdateForecast()
        {
            string sql = "select top(1) LotteryQS,AddTime,LotteryNo from LotteryAnnouncement where LotteryType=1 order by openTime desc";
            List<Model.LotteryAnnouncement> la_list = BLL.LotteryAnnouncement.GetList(sql);
            string qs = la_list[0].LotteryQS;
            string LotteryNo = la_list[0].LotteryNo;
            DateTime AddTime = la_list[0].AddTime;
            //双色球
            string sqlstr = "select MediaUserId,ForecastNo from MediaForecast where LotteryQS='" + qs + "' and LotteryType=1";
            List<Model.MediaForecast> list = GetList(sqlstr);
            if (list != null)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    //string winSSQStr = list[i].LotteryNo;//开奖号码
                    string SSQStr = list[i].ForecastNo;//预测号码
                    long userid = list[i].MediaUserId;//发布预测人账号
                    calculateSSQ(LotteryNo, SSQStr, qs, userid, AddTime);//计算命中情况，更新MediaForecast表
                }
                ComputeForecastNum(1, qs, list);//向MediaForecaststatistics表中加数据
            }

            //大乐透
            string sql2 = "select top(1) LotteryQS ,AddTime,LotteryNo from LotteryAnnouncement where LotteryType=2 order by openTime desc";
            List<Model.LotteryAnnouncement> list_dlt = BLL.LotteryAnnouncement.GetList(sql2);
            string qs2 = list_dlt[0].LotteryQS;
            string LotteryNo2 = list_dlt[0].LotteryNo;
            DateTime AddTime2 = list_dlt[0].AddTime;
            string sqlstr2 = "select MediaUserId,ForecastNo from MediaForecast where LotteryQS='" + qs2 + "' and LotteryType=2";
            List<Model.MediaForecast> list2 = GetList(sqlstr2);
            if (list2 != null)
            {
                for (int i = 0; i < list2.Count; i++)
                {
                    //string winSSQStr = LotteryNo2;
                    string SSQStr = list2[i].ForecastNo;
                    long userid = list2[i].MediaUserId;
                    calculateDLT(LotteryNo2, SSQStr, qs2, userid, AddTime2);//更新媒体预测表

                }
                ComputeForecastNum(2, qs, list2);//大乐透-统计本次发布的预测号码中，各个数字被预测的总次数
            }

        }

        private static int IsExist(string lotteryQs, int lotteryType)//判断本期预测参数是否已存在
        {
            string sql = "select count(Id) from MediaForecaststatistics where LotteryType=" + lotteryType + " and LotteryQS='" + lotteryQs + "'";
            string num = GetPageListBysql(sql).Rows[0][0].ToString();
            return int.Parse(num);
        }
        private static void ComputeForecastNum(int lotteryType, string LotteryQs, List<Model.MediaForecast> list)
        {

            if (lotteryType == 1)
            {
                #region 初始化hashtable
                Hashtable table_red = new Hashtable();
                for (int r = 1; r < 36; r++)//初始化红球表
                {
                    string key1 = "";
                    if (r < 10)
                    {
                        key1 = "0" + r;
                    }
                    else
                    {
                        key1 = r.ToString();
                    }
                    table_red.Add(key1, 0);
                }
                Hashtable table_blue = new Hashtable();
                for (int b = 1; b < 17; b++)//初始化篮球表
                {
                    string key2 = "";
                    if (b < 10)
                    {
                        key2 = "0" + b;
                    }
                    else
                    {
                        key2 = b.ToString();
                    }
                    table_blue.Add(key2, 0);
                }
                #endregion
                string ForecastNo = "";
                int[] rednum_ssq = new int[12];
                int[] bluenum_ssq = new int[3];
                for (int i = 0; i < list.Count; i++)
                {
                    ForecastNo = list[i].ForecastNo;
                    string[] ball = ForecastNo.Split(new char[] { '+' });
                    string redballstr = ball[0];//预测号码中的红球
                    string[] redballparm = redballstr.Split(new char[] { ',' });//预测号码中的红球

                    for (int j = 0; j < 12; j++)
                    {
                        if (table_red.Contains(redballparm[j]))
                        {

                            table_red[redballparm[j]] = (int)table_red[redballparm[j]] + 1;
                        }
                    }
                    string blueballstr = ball[1];//预测号码中的篮球
                    string[] blueballparm = blueballstr.Split(new char[] { ',' });//预测号码中的篮球
                    for (int n = 0; n < 3; n++)
                    {
                        if (table_blue.Contains(blueballparm[n]))
                        {
                            table_blue[blueballparm[n]] = (int)table_blue[blueballparm[n]] + 1;
                        }
                    }
                }
                #region
                //for (int j = 0; j < redballparm.Length; j++)
                //{
                //    if (table_red.ContainsKey(redballparm[j]))
                //    {
                //        table_red.Add(redballparm[j], rednum_ssq[int.Parse(redballparm[j]) - 1]++);
                //    }
                //    else
                //    {
                //        table_red.Add(redballparm[j], rednum_ssq[Convert.ToInt32(redballparm[j]) - 1]);
                //    }
                //}


                //for (int n = 0; n < blueballparm.Length; n++)
                //{
                //    if (table_blue.ContainsKey(blueballparm[n]))
                //    {
                //        table_blue.Add(blueballparm[n], bluenum_ssq[int.Parse(blueballparm[n]) - 1]++);
                //    }
                //    else
                //    {
                //        table_blue.Add(blueballparm[n], bluenum_ssq[int.Parse(blueballparm[n]) - 1]);
                //    }
                //}
                #endregion
                if (IsExist(LotteryQs, 1) == 0)
                {
                    #region 执行添加
                    Model.MediaForecaststatistics forecastModel = new Model.MediaForecaststatistics();
                    forecastModel.LotteryQs = LotteryQs;
                    forecastModel.LotteryType = 1;
                    forecastModel.rnum1 = int.Parse(table_red["01"].ToString());
                    forecastModel.rnum2 = int.Parse(table_red["02"].ToString());
                    forecastModel.rnum3 = int.Parse(table_red["03"].ToString());
                    forecastModel.rnum4 = int.Parse(table_red["04"].ToString());
                    forecastModel.rnum5 = int.Parse(table_red["05"].ToString());
                    forecastModel.rnum6 = int.Parse(table_red["06"].ToString());
                    forecastModel.rnum7 = int.Parse(table_red["07"].ToString());
                    forecastModel.rnum8 = int.Parse(table_red["08"].ToString());
                    forecastModel.rnum9 = int.Parse(table_red["09"].ToString());
                    forecastModel.rnum10 = int.Parse(table_red["10"].ToString());
                    forecastModel.rnum11 = int.Parse(table_red["11"].ToString());
                    forecastModel.rnum12 = int.Parse(table_red["12"].ToString());
                    forecastModel.rnum13 = int.Parse(table_red["13"].ToString());
                    forecastModel.rnum14 = int.Parse(table_red["14"].ToString());
                    forecastModel.rnum15 = int.Parse(table_red["15"].ToString());
                    forecastModel.rnum16 = int.Parse(table_red["16"].ToString());
                    forecastModel.rnum17 = int.Parse(table_red["17"].ToString());
                    forecastModel.rnum18 = int.Parse(table_red["18"].ToString());
                    forecastModel.rnum19 = int.Parse(table_red["19"].ToString());
                    forecastModel.rnum20 = int.Parse(table_red["20"].ToString());
                    forecastModel.rnum21 = int.Parse(table_red["21"].ToString());
                    forecastModel.rnum22 = int.Parse(table_red["22"].ToString());
                    forecastModel.rnum23 = int.Parse(table_red["23"].ToString());
                    forecastModel.rnum24 = int.Parse(table_red["24"].ToString());
                    forecastModel.rnum25 = int.Parse(table_red["25"].ToString());
                    forecastModel.rnum26 = int.Parse(table_red["26"].ToString());
                    forecastModel.rnum27 = int.Parse(table_red["27"].ToString());
                    forecastModel.rnum28 = int.Parse(table_red["28"].ToString());
                    forecastModel.rnum29 = int.Parse(table_red["29"].ToString());
                    forecastModel.rnum30 = int.Parse(table_red["30"].ToString());
                    forecastModel.rnum31 = int.Parse(table_red["31"].ToString());
                    forecastModel.rnum32 = int.Parse(table_red["32"].ToString());
                    forecastModel.rnum33 = int.Parse(table_red["33"].ToString());
                    forecastModel.bnum1 = int.Parse(table_blue["01"].ToString());
                    forecastModel.bnum2 = int.Parse(table_blue["02"].ToString());
                    forecastModel.bnum3 = int.Parse(table_blue["03"].ToString());
                    forecastModel.bnum4 = int.Parse(table_blue["04"].ToString());
                    forecastModel.bnum5 = int.Parse(table_blue["05"].ToString());
                    forecastModel.bnum6 = int.Parse(table_blue["06"].ToString());
                    forecastModel.bnum7 = int.Parse(table_blue["07"].ToString());
                    forecastModel.bnum8 = int.Parse(table_blue["08"].ToString());
                    forecastModel.bnum9 = int.Parse(table_blue["09"].ToString());
                    forecastModel.bnum10 = int.Parse(table_blue["10"].ToString());
                    forecastModel.bnum11 = int.Parse(table_blue["11"].ToString());
                    forecastModel.bnum12 = int.Parse(table_blue["12"].ToString());
                    forecastModel.bnum13 = int.Parse(table_blue["13"].ToString());
                    forecastModel.bnum14 = int.Parse(table_blue["14"].ToString());
                    forecastModel.bnum15 = int.Parse(table_blue["15"].ToString());
                    forecastModel.bnum16 = int.Parse(table_blue["16"].ToString());
                    MediaForecaststatistics.Add(forecastModel);
                    #endregion
                }
            }
            else
            {
                #region 初始化hashtable
                Hashtable table_red = new Hashtable();
                for (int r = 1; r < 36; r++)//初始化红球表
                {
                    string key1 = "";
                    if (r < 10)
                    {
                        key1 = "0" + r;
                    }
                    else
                    {
                        key1 = r.ToString();
                    }
                    table_red.Add(key1, 0);
                }
                Hashtable table_blue = new Hashtable();
                for (int b = 1; b < 17; b++)//初始化篮球表
                {
                    string key2 = "";
                    if (b < 10)
                    {
                        key2 = "0" + b;
                    }
                    else
                    {
                        key2 = b.ToString();
                    }
                    table_blue.Add(key2, 0);
                }
                #endregion
                int[] rednum_dlt = new int[11];
                int[] bluenum_dlt = new int[4];
                string ForecastNo = "";
                for (int i = 0; i < list.Count; i++)
                {
                    ForecastNo = list[i].ForecastNo;
                    string[] ball = ForecastNo.Split(new char[] { '+' });
                    string redballstr = ball[0];//预测红球
                    string[] redballparm = redballstr.Split(new char[] { ',' });
                    for (int j = 0; j < 11; j++)
                    {
                        if (table_red.Contains(redballparm[j]))
                        {
                            table_red[redballparm[j]] = (int)table_red[redballparm[j]] + 1;
                        }
                    }

                    string blueballstr = ball[1];
                    string[] blueballparm = blueballstr.Split(new char[] { ',' });
                    for (int n = 0; n < 3; n++)
                    {
                        if (table_blue.Contains(blueballparm[n]))
                        {
                            table_blue[blueballparm[n]] = (int)table_blue[blueballparm[n]] + 1;
                        }
                    }

                    //DataTable dt = new DataTable();
                    //dt.Columns.Add("htKey", typeof(string));
                    //dt.Columns.Add("htValue", typeof(int));

                    //IDictionaryEnumerator ide = table_red.GetEnumerator();
                    //while (ide.MoveNext())
                    //{
                    //    DataRow dr = dt.NewRow();
                    //    dr["htKey"] = ide.Key.ToString();
                    //    dr["htValue"] = ide.Value.ToString();
                    //    dt.Rows.Add(dr);
                    //}

                    //DataView dv = dt.DefaultView;
                    //dv.Sort = "htValue Desc";//此处是按值降序排列
                    //DataTable dt1 = dv.ToTable();


                    #region
                    //for (int j = 0; j < redballparm.Length; j++)
                    //{
                    //    if (table_red.ContainsKey(redballparm[j]))
                    //    {
                    //        table_red.Add(redballparm[j], rednum_dlt[int.Parse(redballparm[j]) - 1]++);
                    //    }
                    //    else
                    //    {
                    //        table_red.Add(redballparm[j], rednum_dlt[int.Parse(redballparm[j]) - 1]);
                    //    }

                    //}
                    //for (int n = 0; n < blueballparm.Length; n++)
                    //{
                    //    if (table_blue.ContainsKey(blueballparm[n]))
                    //    {
                    //        table_blue.Add(blueballparm[n], bluenum_dlt[int.Parse(blueballparm[n]) - 1]++);
                    //    }
                    //    else
                    //    {
                    //        table_blue.Add(blueballparm[n], bluenum_dlt[int.Parse(blueballparm[n]) - 1]);
                    //    }
                    //}
                    #endregion
                }
                if (IsExist(LotteryQs, 2) == 0)
                {
                    #region 执行添加
                    Model.MediaForecaststatistics forecastModel = new Model.MediaForecaststatistics();
                    forecastModel.LotteryQs = LotteryQs;
                    forecastModel.LotteryType = 2;
                    forecastModel.rnum1 = int.Parse(table_red["01"].ToString());
                    forecastModel.rnum2 = int.Parse(table_red["02"].ToString());
                    forecastModel.rnum3 = int.Parse(table_red["03"].ToString());
                    forecastModel.rnum4 = int.Parse(table_red["04"].ToString());
                    forecastModel.rnum5 = int.Parse(table_red["05"].ToString());
                    forecastModel.rnum6 = int.Parse(table_red["06"].ToString());
                    forecastModel.rnum7 = int.Parse(table_red["07"].ToString());
                    forecastModel.rnum8 = int.Parse(table_red["08"].ToString());
                    forecastModel.rnum9 = int.Parse(table_red["09"].ToString());
                    forecastModel.rnum10 = int.Parse(table_red["10"].ToString());
                    forecastModel.rnum11 = int.Parse(table_red["11"].ToString());
                    forecastModel.rnum12 = int.Parse(table_red["12"].ToString());
                    forecastModel.rnum13 = int.Parse(table_red["13"].ToString());
                    forecastModel.rnum14 = int.Parse(table_red["14"].ToString());
                    forecastModel.rnum15 = int.Parse(table_red["15"].ToString());
                    forecastModel.rnum16 = int.Parse(table_red["16"].ToString());
                    forecastModel.rnum17 = int.Parse(table_red["17"].ToString());
                    forecastModel.rnum18 = int.Parse(table_red["18"].ToString());
                    forecastModel.rnum19 = int.Parse(table_red["19"].ToString());
                    forecastModel.rnum20 = int.Parse(table_red["20"].ToString());
                    forecastModel.rnum21 = int.Parse(table_red["21"].ToString());
                    forecastModel.rnum22 = int.Parse(table_red["22"].ToString());
                    forecastModel.rnum23 = int.Parse(table_red["23"].ToString());
                    forecastModel.rnum24 = int.Parse(table_red["24"].ToString());
                    forecastModel.rnum25 = int.Parse(table_red["25"].ToString());
                    forecastModel.rnum26 = int.Parse(table_red["26"].ToString());
                    forecastModel.rnum27 = int.Parse(table_red["27"].ToString());
                    forecastModel.rnum28 = int.Parse(table_red["28"].ToString());
                    forecastModel.rnum29 = int.Parse(table_red["29"].ToString());
                    forecastModel.rnum30 = int.Parse(table_red["30"].ToString());
                    forecastModel.rnum31 = int.Parse(table_red["31"].ToString());
                    forecastModel.rnum32 = int.Parse(table_red["32"].ToString());
                    forecastModel.rnum33 = int.Parse(table_red["33"].ToString());
                    forecastModel.rnum34 = int.Parse(table_red["34"].ToString());
                    forecastModel.rnum35 = int.Parse(table_red["35"].ToString());
                    forecastModel.bnum1 = int.Parse(table_blue["01"].ToString());
                    forecastModel.bnum2 = int.Parse(table_blue["02"].ToString());
                    forecastModel.bnum3 = int.Parse(table_blue["03"].ToString());
                    forecastModel.bnum4 = int.Parse(table_blue["04"].ToString());
                    forecastModel.bnum5 = int.Parse(table_blue["05"].ToString());
                    forecastModel.bnum6 = int.Parse(table_blue["06"].ToString());
                    forecastModel.bnum7 = int.Parse(table_blue["07"].ToString());
                    forecastModel.bnum8 = int.Parse(table_blue["08"].ToString());
                    forecastModel.bnum9 = int.Parse(table_blue["09"].ToString());
                    forecastModel.bnum10 = int.Parse(table_blue["10"].ToString());
                    forecastModel.bnum11 = int.Parse(table_blue["11"].ToString());
                    forecastModel.bnum12 = int.Parse(table_blue["12"].ToString());
                    MediaForecaststatistics.Add(forecastModel);
                    #endregion
                }

            }
        }

        /// <summary>
        /// 双色球，预测对比
        /// </summary>
        /// <param name="winSSQStr">开奖号码</param>
        /// <param name="SSQStr">用户购买的号码</param>
        /// <param name="lotteryQs">期数</param>
        /// <param name="userid">发布预测的用户ID</param>
        /// <param name="AddTime">开奖时间</param>
        /// <returns>等级</returns>
        private static void calculateSSQ(string winSSQStr, string SSQStr, string lotteryQs, long userid, DateTime AddTime)
        {
            int level = 0;

            string zred = "";//命中的红球
            string zblue = "";//命中的篮球
            //中奖号
            string[] winSSQList = winSSQStr.Split('+');
            string[] winRed = winSSQList[0].Split(',');
            string[] winBlue = winSSQList[1].Split(',');

            //用户发布的号码
            //for (int n = 0; n < SSQStr.Rows.Count; n++)
            //{
            string[] SSQList = SSQStr.Split('+');
            string[] UserRed = SSQList[0].Split(',');
            string[] UserBlue = SSQList[1].Split(',');

            int redCount = 0;
            int blueCount = 0;
            //比对红球
            for (int i = 0; i < winRed.Length; i++)
            {
                for (int j = 0; j < UserRed.Length; j++)
                {
                    if (int.Parse(winRed[i]) == int.Parse(UserRed[j]))
                    {
                        redCount++;
                        zred += UserRed[j] + ",";
                        break;
                    }
                }
            }
            if (redCount == 0)
            {
                zred = "";
            }
            else
            {
                zred = zred.Substring(0, zred.Length - 1);
            }

            //比对蓝球
            for (int i = 0; i < winBlue.Length; i++)
            {
                for (int j = 0; j < UserBlue.Length; j++)
                {
                    if (int.Parse(winBlue[i]) == int.Parse(UserBlue[j]))
                    {
                        blueCount++;
                        zblue += UserBlue[j] + ",";
                        break;
                    }
                }
            }
            if (blueCount == 0)
            {
                zblue = "";

            }
            else
            {
                zblue = zblue.Substring(0, zblue.Length - 1);
            }

            if (redCount == 6 && blueCount == 1)
            {
                level = 1;

            }
            else if (redCount == 6 && blueCount == 0)
            {
                level = 2;

            }
            else if (redCount == 5 && blueCount == 1)
            {
                level = 3;

            }
            else if ((redCount == 5 && blueCount == 0) || (redCount == 4 && blueCount == 1))
            {
                level = 4;

            }
            else if ((redCount == 4 && blueCount == 0) || (redCount == 3 && blueCount == 1))
            {
                level = 5;

            }
            else if ((redCount == 2 || redCount == 1 || redCount == 0) && blueCount == 1)
            {
                level = 6;

            }
            else
            {
                level = -1;

            }
            //}

            Data_Newscont.Update1(zred, zblue, level, userid, 1, lotteryQs, winSSQStr, AddTime);
        }
        /// <summary>
        /// 大乐透,预测对比
        /// </summary>
        /// <param name="winSSQStr">开奖号码</param>
        /// <param name="SSQStr">用户购买的号码</param>
        /// <returns>等级</returns>
        private static void calculateDLT(string winSSQStr, string SSQStr, string lotteryQs, long userid, DateTime AddTime)
        {
            int level = 0;

            string zred = "";//命中的红球
            string zblue = "";//命中的篮球
                              //中奖号
            string[] winSSQList = winSSQStr.Split('+');
            string[] winRed = winSSQList[0].Split(',');
            string[] winBlue = winSSQList[1].Split(',');
            //用户号码
            //SSQStr = SSQStr.Replace("$", "*");
            //string[] DLT = SSQStr.Split(',');
            //for (int n = 0; n < DLT.Length; n++)
            //{
            string[] SSQList = SSQStr.Split('+');
            string[] UserRed = SSQList[0].Split(',');
            string[] UserBlue = SSQList[1].Split(',');

            int redCount = 0;
            int blueCount = 0;
            //比对红球
            for (int i = 0; i < winRed.Length; i++)
            {
                for (int j = 0; j < UserRed.Length; j++)
                {
                    if (int.Parse(winRed[i]) == int.Parse(UserRed[j]))
                    {
                        redCount++;
                        zred += UserRed[j] + ",";
                        break;
                    }
                }
            }
            if (redCount == 0)
            {
                zred = "";
            }
            else
            {
                zred = zred.Substring(0, zred.Length - 1);
            }

            //比对蓝球
            for (int i = 0; i < winBlue.Length; i++)
            {
                for (int j = 0; j < UserBlue.Length; j++)
                {
                    if (int.Parse(winBlue[i]) == int.Parse(UserBlue[j]))
                    {
                        blueCount++;
                        zblue += UserBlue[j] + ",";
                        break;
                    }
                }
            }
            if (blueCount == 0)
            {
                zblue = "";
            }
            else
            {
                zblue = zblue.Substring(0, zblue.Length - 1);
            }

            if (redCount == 5 && blueCount == 2)
            {
                level = 1;
            }
            else if (redCount == 5 && blueCount == 1)
            {
                level = 2;
            }
            else if ((redCount == 5 && blueCount == 0) || (redCount == 4 && blueCount == 2))
            {
                level = 3;
            }
            else if ((redCount == 4 && blueCount == 1) || (redCount == 3 && blueCount == 2))
            {
                level = 4;
            }
            else if ((redCount == 4 && blueCount == 0) || (redCount == 3 && blueCount == 1) || (redCount == 2 && blueCount == 2))
            {
                level = 5;
            }
            else if ((redCount == 3 && blueCount == 0) || (redCount == 1 && blueCount == 2) || (redCount == 2 && blueCount == 1) || (redCount == 0 && blueCount == 2))
            {
                level = 6;
            }
            else
            {
                level = -1;
            }
            //}
            Data_Newscont.Update1(zred, zblue, level, userid, 2, lotteryQs, winSSQStr, AddTime);

        }

        public static string[] SplitString(string ForecastNo)
        {
            string[] ball = ForecastNo.Split(new char[] { '+' });
            return ball;
        }
        public static int RedNum(string Mredball)//红球
        {
            string rednum = Mredball.Replace(",", "");
            return rednum.Length / 2;
        }
        public static int BlueNum(string Mblueball)//篮球
        {
            string bluenum = Mblueball.Replace(",", "");
            return bluenum.Length / 2;
        }

        public static double ComputeHistoryWinRute(long userId, string LotteryType)//历史中奖率
        {
            string sql2 = "select count(*) from MediaForecast where MediaUserId='" + userId + "' and LotteryType=" + LotteryType + "";
            List<Model.MediaForecast> alldatalist = BLL.MediaForecast.GetList(sql2);
            List<Model.MediaForecast> windatalist = new List<Model.MediaForecast>();
            foreach (Model.MediaForecast item in alldatalist)
            {
                if (item.WinLevel > 0)
                {
                    windatalist.Add(item);
                }
            }
            int wincount = windatalist.Count;
            double winrute = 0;
            double HistoryWinRute;
            if (alldatalist.Count > 0)
            {
                winrute = wincount / alldatalist.Count;
                HistoryWinRute = Math.Round(winrute, 2) * 100;
            }
            else
            {
                HistoryWinRute = Convert.ToDouble(0);
            }
            return HistoryWinRute;
        }
        public static Model.MediaForecast_Convert HistoryMost_MZ(string LotteryType, long userid)//历史最高命中红球和篮球
        {
            string sql2 = "select * from MediaForecast where LotteryType=" + LotteryType + " and MediaUserId = '" + userid + "' order by len(Mredball) desc,LEN(Mblueball) desc";
            List<Model.MediaForecast> alldatalist = BLL.MediaForecast.GetList(sql2);
            Model.MediaForecast_Convert mc = new Model.MediaForecast_Convert();
            if (alldatalist[0].Mredball == null)
            {
                mc.HistroyMZ_Most_red = 0;
            }
            else
            {
                mc.HistroyMZ_Most_red = RedNum(alldatalist[0].Mredball);
            }
            if (alldatalist[0].Mblueball == null)
            {
                mc.HistroyMZ_Most_blue = 0;
            }
            else
            {
                mc.HistroyMZ_Most_blue = BlueNum(alldatalist[0].Mblueball);
            }



            return mc;
        }
        public static Model.MediaForecast_Convert ComputeRedBallWin(List<Model.MediaForecast> list)
        {
            Model.MediaForecast_Convert mc = new Model.MediaForecast_Convert();
            int count = list.Count;//数据总个数
            int allRedcount = 12 * count;//红球总个数
            int allBluecount = 3 * count;
            string MRed = "";
            string MBlue = "";
            int level1 = 0, level2 = 0, level3 = 0, level4 = 0, level5 = 0, level6 = 0;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Mredball == null || list[i].Mredball == "")
                {
                    MRed = "";
                }
                else
                {
                    MRed += list[i].Mredball.Replace(",", " ");//
                }
                if (list[i].Mblueball == null || list[i].Mblueball == "")
                {
                    MBlue = "";
                }
                else
                { MBlue += list[i].Mblueball.Replace(",", " "); }



                if (list[i].WinLevel == 1)
                {
                    level1 = level1 + 1;
                }
                else if (list[i].WinLevel == 2)
                {
                    level2 = level2 + 1;
                }
                else if (list[i].WinLevel == 3)
                {
                    level3 = level3 + 1;
                }
                else if (list[i].WinLevel == 4)
                {
                    level4 = level4 + 1;
                }
                else if (list[i].WinLevel == 5)
                {
                    level5 = level5 + 1;
                }
                else if (list[i].WinLevel == 6)
                {
                    level6 = level6 + 1;
                }
            }
            int MRedCount = MRed.Length / 2;//红球命中总个数
            int MBlueCount = MBlue.Length / 2;
            mc.RedBall_AverageMZCount = MRedCount / allRedcount;
            mc.BlueBall_AverageMZRate = MBlueCount / allBluecount;
            string str = "";
            if (level1 > 0)
            {
                str = "一等奖" + level1 + "次,";
            }
            if (level2 > 0)
            {
                str += "二等奖" + level2 + "次,";
            }
            if (level3 > 0)
            {
                str += "三等奖" + level3 + "次,";
            }
            if (level4 > 0)
            {
                str += "四等奖" + level4 + "次,";
            }
            if (level5 > 0)
            {
                str += "五等奖" + level5 + "次,";
            }
            if (level6 > 0)
            {
                str += "六等奖" + level6 + "次";
            }
            mc.MZ_Most = str;
            return mc;
        }




    }


}
